﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Entity;

namespace Staionery_Inventory.Presentation.Reports
{
    public partial class StationeryReqTrendAnalysis : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void rbCustoms_CheckedChanged(object sender, EventArgs e)
        {
            if (rbCustoms.Checked == true)
            {
                ddlMonth1.Enabled = true;
                ddlMonth2.Enabled = true;
                ddlMonth3.Enabled = true;
                ddlYear1.Enabled = true;
                ddlYear2.Enabled = true;
                ddlYear3.Enabled = true;
                Lblmonth1.Enabled = true;
                lblMonth3.Enabled = true;
                lblMonth2.Enabled = true;
                lblYear2.Enabled = true;
                lblYear3.Enabled = true;
                lblYear1.Enabled = true;
            }
        }



        protected void rbLast3Months_CheckedChanged(object sender, EventArgs e)
        {
            if (rbLast3Months.Checked == true)
            {
                ddlMonth1.Enabled = false;
                ddlMonth2.Enabled = false;
                ddlMonth3.Enabled = false;
                ddlYear1.Enabled = false;
                ddlYear2.Enabled = false;
                ddlYear3.Enabled = false;
                Lblmonth1.Enabled = false;
                lblMonth3.Enabled = false;
                lblMonth2.Enabled = false;
                lblYear2.Enabled = false;
                lblYear3.Enabled = false;
                lblYear1.Enabled = false;

                ddlMonth1.SelectedIndex = System.DateTime.Now.Month - 1;
                ddlYear1.SelectedIndex = 2014 - System.DateTime.Now.Year;
                if (System.DateTime.Now.Month >= 2)
                {
                    ddlMonth2.SelectedIndex = System.DateTime.Now.Month - 2;
                    ddlYear2.SelectedIndex = 2014 - System.DateTime.Now.Year;
                }
                else
                {
                    ddlMonth2.SelectedIndex = System.DateTime.Now.Month + 12 - 2;
                    ddlYear2.SelectedIndex = 2014 - System.DateTime.Now.Year - 1;
                }

                if (System.DateTime.Now.Month >= 3)
                {
                    ddlMonth3.SelectedIndex = System.DateTime.Now.Month - 3;
                    ddlYear3.SelectedIndex = 2014 - System.DateTime.Now.Year;
                }
                else
                {
                    ddlMonth2.SelectedIndex = System.DateTime.Now.Month + 12 - 3;
                    ddlYear2.SelectedIndex = 2014 - System.DateTime.Now.Year - 1;
                }
                
            }


        }
        protected void rbQuantity_CheckedChanged(object sender, EventArgs e)
        {
            if (rbQuantity.Checked == true)
            {
                Ddldept.Enabled = true;
                rbLast3Months.Enabled = true;
                rbCustoms.Enabled = true;
                lblDepartment.Enabled = true;
                lbloptions.Enabled = true;

            }
        }

        protected void Inventory_CheckedChanged(object sender, EventArgs e)
        {
            if (rbInventory.Checked == true)
            {
                Ddldept.Enabled = false;
                rbLast3Months.Enabled = false;
                rbCustoms.Enabled = false;
                lblDepartment.Enabled = false;
                lbloptions.Enabled = false;

            }
        }

        [WebMethod]
        public static List<ReportModel> getChartData(string jdeptName, int jmonth1, int jyear1, int jmonth2, int jyear2, int jmonth3, int jyear3, string joptionsCategory, string[] jselectedCategory)
        {
            InvManagementEntities em = new InvManagementEntities();

            if (joptionsCategory == "category")
            {
                string[] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
                int month1 = jmonth1;
                int year1 = jyear1;
                DateTime selectedatetime1 = new DateTime(jyear1, month1, 1);
                DateTime startdate1 = new DateTime(jyear1, month1, 1);
                DateTime enddate1 = new DateTime();

                var thismonth1 = startdate1.AddDays(1 - selectedatetime1.Day);
                startdate1 = Convert.ToDateTime(thismonth1);
                var thismonend1 = thismonth1.AddMonths(1).AddSeconds(-1);
                enddate1 = Convert.ToDateTime(thismonend1);

                List<String> li = new List<string>();
                foreach (string cat in jselectedCategory)
                    li.Add(cat);

                var query1 = (from abcd in em.SRFDetails
                              join abcde in em.StationeryItems on abcd.ItemID equals abcde.ItemID
                              where li.Contains(abcd.StationeryItem.Category) && abcd.Department.DeptName == jdeptName && (abcd.SRF.RFDate >= startdate1 && abcd.SRF.RFDate <= enddate1)
                              group abcd by abcd.StationeryItem.Category into d
                              select new ReportModelTemp { xMember = d.Key, series = (int)d.Sum(x => x.QtyRetrieved) });
                List<ReportModelTemp> temp1 = query1.ToList<ReportModelTemp>();

                int month2 = jmonth2;
                int year2 = jyear2;
                DateTime selectedatetime2 = new DateTime(year2, month2, 1);
                DateTime startdate2 = new DateTime(year2, month2, 1);
                DateTime enddate2 = new DateTime();

                var thismonth2 = startdate2.AddDays(1 - selectedatetime2.Day);
                startdate2 = Convert.ToDateTime(thismonth2);
                var thismonend2 = thismonth2.AddMonths(1).AddSeconds(-1);
                enddate2 = Convert.ToDateTime(thismonend2);


                var query2 = (from abcd in em.SRFDetails
                              join abcde in em.StationeryItems on abcd.ItemID equals abcde.ItemID
                              where li.Contains(abcd.StationeryItem.Category) && abcd.Department.DeptName == jdeptName && (abcd.SRF.RFDate >= startdate2 && abcd.SRF.RFDate <= enddate2)
                              group abcd by abcd.StationeryItem.Category into d
                              select new ReportModelTemp { xMember = d.Key, series = (int)d.Sum(x => x.QtyRetrieved) });

                List<ReportModelTemp> temp2 = query2.ToList<ReportModelTemp>();

                int month3 = jmonth3;
                int year3 = jyear3;
                DateTime selectedatetime3 = new DateTime(year3, month3, 1);
                DateTime startdate3 = new DateTime(year3, month3, 1);
                DateTime enddate3 = new DateTime();

                var thismonth3 = startdate3.AddDays(1 - selectedatetime3.Day);
                startdate3 = Convert.ToDateTime(thismonth3);
                var thismonend3 = thismonth3.AddMonths(1).AddSeconds(-1);
                enddate3 = Convert.ToDateTime(thismonend3);

                var query3 = (from abcd in em.SRFDetails
                              join abcde in em.StationeryItems on abcd.ItemID equals abcde.ItemID
                              where li.Contains(abcd.StationeryItem.Category) && abcd.Department.DeptName == jdeptName && (abcd.SRF.RFDate >= startdate3 && abcd.SRF.RFDate <= enddate3)
                              group abcd by abcd.StationeryItem.Category into d
                              select new ReportModelTemp { xMember = d.Key, series = (int)d.Sum(x => x.QtyRetrieved) });

                List<ReportModelTemp> temp3 = query3.ToList<ReportModelTemp>();

                List<ReportModel> reportList = new List<ReportModel>();
                foreach (ReportModelTemp r in temp1)
                {
                    ReportModel temp = new ReportModel();
                    temp.XMember = r.XMember;
                    temp.Series1 = r.Series;
                    temp.Series2 = 0;
                    temp.Series3 = 0;
                    reportList.Add(temp);
                }
                foreach (ReportModelTemp r in temp2)
                {
                    foreach (ReportModel temp in reportList)
                    {
                        if (temp.XMember == r.XMember)
                        {
                            temp.Series2 = r.Series;
                        }
                    }

                }
                foreach (ReportModelTemp r in temp3)
                {
                    foreach (ReportModel temp in reportList)
                    {
                        if (temp.XMember == r.XMember)
                        {
                            temp.Series3 = r.Series;
                        }
                    }
                }

                string series1 = monthName[jmonth1 - 1] + " " + jyear1.ToString();
                string series2 = monthName[jmonth2 - 1] + " " + jyear2.ToString();
                string series3 = monthName[jmonth3 - 1] + " " + jyear3.ToString();

                if (temp1.Count > 0)
                {
                    reportList[0].Series1Name = series1;
                    reportList[0].Series2Name = series2;
                    reportList[0].Series3Name = series3;
                }
                return reportList;
            }
            else
            {
                List<String> li = new List<string>();
                foreach (string cat in jselectedCategory)
                    li.Add(cat);

                var query = (from i in em.StationeryItems
                             where li.Contains(i.Category)
                             group i by i.Category into g
                             select new ReportModelTemp { xMember = g.Key, series = (int)g.Sum(x => x.QtyLeft) });

                List<ReportModelTemp> tempList = query.ToList<ReportModelTemp>();
                List<ReportModel> reportList = new List<ReportModel>();
                foreach (ReportModelTemp temp in tempList)
                {
                    ReportModel model = new ReportModel();
                    model.xMember = temp.xMember;
                    model.series1 = temp.series;
                    reportList.Add(model);
                }
                reportList[0].Series1Name = "Quantity by Category";
                return reportList;
            }
        }
    }
}